using UnityEngine;
using UnityEngine.UI;

[ExecuteInEditMode]
public class AnchorImageText : MonoBehaviour
{
	private RectTransform rectTransform;

	public Image image;

	public Text text;

	public HAlign horizontalAlign = HAlign.center;

	public float spaceBetween = 12f;

	private Vector2 currentImageSize;

	private Vector2 currentTextSize;

	private Vector2 currentAnchorSize;

	private void Start()
	{
		currentImageSize = Vector2.zero;
		currentTextSize = Vector2.zero;
		currentAnchorSize = Vector2.zero;
		GenericDialog.DialogEnabledEvent += OnDialogEnabled;
	}

	private void Destoy()
	{
		GenericDialog.DialogEnabledEvent -= OnDialogEnabled;
	}

	private void OnDialogEnabled()
	{
		DoAlign();
	}

	private void Update()
	{
		if (currentTextSize != text.rectTransform.sizeDelta)
		{
			DoAlign();
		}
	}

	private void DoAlign()
	{
		if (rectTransform == null)
		{
			rectTransform = GetComponent<RectTransform>();
		}
		currentImageSize = image.rectTransform.sizeDelta;
		currentTextSize = text.rectTransform.sizeDelta;
		currentAnchorSize = rectTransform.sizeDelta;
		Vector2 anchoredPosition = image.rectTransform.anchoredPosition;
		Vector2 anchoredPosition2 = text.rectTransform.anchoredPosition;
		float num = currentImageSize.x + spaceBetween + currentTextSize.x;
		if (horizontalAlign == HAlign.center)
		{
			anchoredPosition.x = rectTransform.anchoredPosition.x - num / 2f + currentImageSize.x / 2f;
			anchoredPosition2.x = rectTransform.anchoredPosition.x + num / 2f - currentTextSize.x / 2f;
			anchoredPosition.y = rectTransform.anchoredPosition.y;
			anchoredPosition2.y = rectTransform.anchoredPosition.y;
		}
		else if (horizontalAlign == HAlign.left)
		{
			anchoredPosition.x = rectTransform.anchoredPosition.x - currentAnchorSize.x / 2f + currentImageSize.x / 2f;
			anchoredPosition2.x = rectTransform.anchoredPosition.x - currentAnchorSize.x / 2f + currentImageSize.x + spaceBetween + currentTextSize.x / 2f;
			anchoredPosition.y = rectTransform.anchoredPosition.y;
			anchoredPosition2.y = rectTransform.anchoredPosition.y;
		}
		else if (horizontalAlign == HAlign.right)
		{
			anchoredPosition.x = rectTransform.anchoredPosition.x + currentAnchorSize.x / 2f - currentTextSize.x - spaceBetween - currentImageSize.x / 2f;
			anchoredPosition2.x = rectTransform.anchoredPosition.x + currentAnchorSize.x / 2f - currentTextSize.x / 2f;
			anchoredPosition.y = rectTransform.anchoredPosition.y;
			anchoredPosition2.y = rectTransform.anchoredPosition.y;
		}
		image.rectTransform.anchoredPosition = anchoredPosition;
		text.rectTransform.anchoredPosition = anchoredPosition2;
	}
}
